// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import CustomButton from "../../BASE/CButton";
import ComponentBase from "../../BASE/ComponentBase";
import { gif } from "../../BASE/spineANDgif/gif";
import GeZi from "../../Ditu/Gezi/GeZi";
import AnimalManager from "../../Manager/AnimalManager";
import GeZiManager from "../../Manager/GeZiManager";
import KHD from "../../Message/KHD";
import { Message } from "../../Message/Message";
import MessageCenter from "../../Message/MessageCenter";
import SXX from "../../Scharacter/SXX";
import player from "../../game/player";
import state from "../../game/time/state";
import turn from "../../game/time/turn";
import KOG from "../KOG";
import NWshopM from "./NWshopM";
import shopM from "./ShopM";
import Shops from "./shops";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NWshopUI extends ComponentBase {



    Sk:number=0;
    SUI:cc.Node=null;
    A1:cc.Node=null;
    A2:cc.Node=null;
    A3:cc.Node=null;
    A4:cc.Node=null;
   A5:cc.Node=null;
    XJ:cc.Node=null;
    A7:cc.Node=null;
    Dqizi:cc.Node=null;
    DL:cc.Node=null;
    MU:cc.Node=null;
    NWj:cc.Node=null;
    leveUP:cc.Node=null;
    changL:cc.Node=null;
    AM:cc.Node=null
    TOU:cc.Node=null;
    @property({ type: cc.SpriteAtlas })
    Atlas: cc.SpriteAtlas = null;
    @property({ type: cc.SpriteAtlas })
    Atlas2: cc.SpriteAtlas = null;
    @property({ type: cc.SpriteAtlas })
    Atlas3: cc.SpriteAtlas = null;
yes:cc.Node;
DW:number=0;
i:number=1
KTT:number=3;
onLoad(): void {
    this.MU=cc.find("Canvas/UIManager/magicUI");

   // this.SUI= cc.find("Canvas/UIManager/shopUI/1L/A1")
 this.A1= cc.find("Canvas/UIManager/NWshop/1L/pu1")
 this.A2= cc.find("Canvas/UIManager/NWshop/1L/pu2")
 this.A3= cc.find("Canvas/UIManager/NWshop/1L/pu3")
 this.A4= cc.find("Canvas/UIManager/NWshop/1L/pu4")
 this.XJ= cc.find("Canvas/UIManager/NWshop/NG")
 this.A5= cc.find("Canvas/UIManager/NWshop/1L/pu5")
this.NWj=cc.find("Canvas/UIManager/NWshop/2L/NW/NW")
 this.AM=cc.find("Canvas/DituManager/New Node/AnimalManager")
 this.TOU=cc.find("Canvas/UIManager/TOUI")
// this.TOU.active=false
}
shop(){    
 

    MessageCenter.MakeMessage("UIManager","change",3.5);
    this.ST();
    
   }




shopX(){
    MessageCenter.MakeMessage("UIManager","change",1);


//GeZiManager.PCP.ST();
}

TOUI(n:number){
let k=GeZiManager.PCP.getComponent(SXX).getTO(n)
    this.TOU.active=true;
    this.TOU.children[0].children[0].getComponent(cc.Label).string=k[0]
   
    this.TOU.children[1].children[0].getComponent(cc.Label).string=k[1]
    this.TOU.children[2].children[0].getComponent(cc.Label).string=k[2]
    NWshopM.NWN[0]=k[0]
    NWshopM.NWN[1]=k[1]
    NWshopM.NWN[2]=k[2]
}
NG(){

    GeZiManager.qing()
        MessageCenter.MakeMessage("AM",turn.turn,"getZB")
        GeZiManager.FTPN=3.5;
    GeZiManager.JL=4;
       state.ST=2;
               
                
       
       MessageCenter.MakeMessage("UIManager","change",-1);
     
    
       for(let manager of GeZiManager.YuanGong){
      
      manager.getFar(GeZiManager.JL);
    if(GeZiManager.BanMove.includes(manager.ZB)||GeZiManager.qins.includes(manager.ZB)){
         manager.red()}
         if(GeZiManager.AllObstacles[0]!=null){
         for(let a of GeZiManager.AllObstacles){
          if(a.ZB==manager.ZB&&!(GeZiManager.grass.includes(a.ZB)&&a.ObstaclesName!="qizi")){manager.red()}
      if(a.tt=="flag"&&a.FW.includes(manager.ZB)&&a.color!=GeZiManager.PCP.color&&a.color!="S02"){manager.red()}
        
        }}
    }
    
    }


NW(){
    console.log(NWshopM.NWN)

 

NWshopM.NWN=NWshopM.get5(5)
console.log(NWshopM.NWN[0])
console.log( this.AM.getComponent(Shops))
    this.A1.children[0].getComponent(cc.Sprite).spriteFrame= this.AM.getComponent(Shops).find(10,NWshopM.NWN[0].toString())
    KOG.DSP( this.A1.children[0],NWshopM.NWN[0].toString())
    this.A2.children[0].getComponent(cc.Sprite).spriteFrame= this.AM.getComponent(Shops).find(10,NWshopM.NWN[1].toString())
    KOG.DSP( this.A2.children[0],NWshopM.NWN[1].toString())
    this.A3.children[0].getComponent(cc.Sprite).spriteFrame= this.AM.getComponent(Shops).find(10,NWshopM.NWN[2].toString())
    KOG.DSP( this.A3.children[0],NWshopM.NWN[2].toString())
    this.A4.children[0].getComponent(cc.Sprite).spriteFrame= this.AM.getComponent(Shops).find(10,NWshopM.NWN[3].toString())
    KOG.DSP( this.A4.children[0],NWshopM.NWN[3].toString())
    this.A5.children[0].getComponent(cc.Sprite).spriteFrame= this.AM.getComponent(Shops).find(10,NWshopM.NWN[4].toString())
    KOG.DSP( this.A5.children[0],NWshopM.NWN[4].toString())
    this.A1.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.NWN[0]
    this.A2.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.NWN[1]
    this.A3.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.NWN[2]
    this.A4.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.NWN[3]
    this.A5.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.NWN[4]

    MessageCenter.MakeGMessage("NWSU",[GeZiManager.PCP.ZB],NWshopM.NWN[4],GeZiManager.PCP.Pturn,"NW")

    this.ST();
    
}

NWF(n:number){
    console.log(NWshopM.NWN)

 
  this.TOU.active=true;

let k
switch (n) {
    case 1:k=GeZiManager.PCP.getComponent(SXX).JX1
        
        break;
        case 2:k=GeZiManager.PCP.getComponent(SXX).JX2
        
        break;
        case 3:k=GeZiManager.PCP.getComponent(SXX).JX3
        
        break;

    default:k=GeZiManager.PCP.getComponent(SXX).JX1
        break;
}
k=this.mArr1Arr2(k,k,k,k,k,k,k)
    NWshopM.NWN=this.getRAA(k,5)
    console.log(NWshopM.NWN)
        this.A1.children[0].getComponent(cc.Sprite).spriteFrame= this.AM.getComponent(Shops).find(2,NWshopM.NWN[0].toLowerCase())
        this.A2.children[0].getComponent(cc.Sprite).spriteFrame= this.AM.getComponent(Shops).find(2,NWshopM.NWN[1].toLowerCase())
        this.A3.children[0].getComponent(cc.Sprite).spriteFrame= this.AM.getComponent(Shops).find(2,NWshopM.NWN[2].toLowerCase())
        this.A4.children[0].getComponent(cc.Sprite).spriteFrame= this.AM.getComponent(Shops).find(2,NWshopM.NWN[3].toLowerCase())
        this.A5.children[0].getComponent(cc.Sprite).spriteFrame= this.AM.getComponent(Shops).find(2,NWshopM.NWN[4].toLowerCase())
        this.A1.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.NWN[0]
        this.A2.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.NWN[1]
        this.A3.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.NWN[2]
        this.A4.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.NWN[3]
        this.A5.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.NWN[4]
    
        //MessageCenter.MakeGMessage("NWSU",[GeZiManager.PCP.ZB],NWshopM.NWN[4],GeZiManager.PCP.Pturn,"NW")
    
        this.ST();





}
YFH(){let o ,j ;
    j=[]
if (GeZiManager.PCP.color=="Blue") {
  o=[GeZiManager.P1,GeZiManager.P3,GeZiManager.P5]
}else {o=[GeZiManager.P2,GeZiManager.P4,GeZiManager.P6]
}
for(let a of o){

if (a.LST<0) {
    j.push(a.Pturn)
}

}
console.log(j)

for (let index = 1; index <= 6; index++) {
    this.node.children[5].children[index].active=false
     
 }
for (let index = 0; index < j.length; index++) {
   this.node.children[5].children[j[index]].active=true
    
}

}

fh1(){

 this.NWFH(1)   
}


fh2(){

    this.NWFH(2)   
   }

   fh3(){

    this.NWFH(3)   
   }

   fh4(){

    this.NWFH(4)   
   }

   fh5(){

    this.NWFH(5)   
   }

   fh6(){

    this.NWFH(6)   
   }
               




NWFH(n:number){
state.ST=0.5
switch (n) {
    case 1:GeZiManager.Tcharacter=GeZiManager.P1
        
        break;
        case 2:GeZiManager.Tcharacter=GeZiManager.P2
        
        break;
        case 3:GeZiManager.Tcharacter=GeZiManager.P3
        
        break;
        case 4:GeZiManager.Tcharacter=GeZiManager.P4
        
        break;
        case 5:GeZiManager.Tcharacter=GeZiManager.P5
        
        break;
        case 6:GeZiManager.Tcharacter=GeZiManager.P6
        
        break;
    default:
        break;
}
    MessageCenter.MakeMessage("UIManager","change",-1);

    for(let manager of GeZiManager.YuanGong){
let a=new Message("",[65],-1)
        manager.ReceiveMessage(a) ;   
}
MessageCenter.MakeMessage("AM",turn.turn,"getZB")

GeZiManager.GeZis =GeZiManager.getF(GeZiManager.PCP.ZB,3)

let c=new Message("",GeZiManager.GeZis,1);
for (let m of GeZiManager.YuanGong) {


 
   // manager.getFar(n+GeZiManager.JL);
        
            m.red();
        
           m.ReceiveMessage(c);
      
if(GeZiManager.BanMove.includes(m.ZB)){
m.out();}
console.log(GeZiManager.Tcharacter)
}

  



}


dd1(){

    MessageCenter.MakeGMessage("NWSU",[GeZiManager.PCP.ZB],NWshopM.NWN[0],GeZiManager.PCP.Pturn,"buy")
//MessageCenter.MakeMessage("BUI",[3],"Ban");

//GeZiManager.PCP.KTT-=1;
this.TOU.active=false
MessageCenter.MakeGMessage("GM",[0], turn.turn+1, turn.turn,"PASS"); 

}
dd2(){

    MessageCenter.MakeGMessage("NWSU",[GeZiManager.PCP.ZB],NWshopM.NWN[1],GeZiManager.PCP.Pturn,"buy")
//MessageCenter.MakeMessage("BUI",[3],"Ban");

//GeZiManager.PCP.KTT-=1;
this.TOU.active=false
MessageCenter.MakeGMessage("GM",[0], turn.turn+1, turn.turn,"PASS"); 

}
dd3(){

    MessageCenter.MakeGMessage("NWSU",[GeZiManager.PCP.ZB],NWshopM.NWN[2],GeZiManager.PCP.Pturn,"buy")
//MessageCenter.MakeMessage("BUI",[3],"Ban");

//GeZiManager.PCP.KTT-=1;
this.TOU.active=false
MessageCenter.MakeGMessage("GM",[0], turn.turn+1, turn.turn,"PASS"); 

}


dA1(){
console.log(NWshopM.NWN[0])
    MessageCenter.MakeGMessage("NWSU",[GeZiManager.PCP.ZB],NWshopM.NWN[0],GeZiManager.PCP.Pturn,"buy")
//MessageCenter.MakeMessage("BUI",[3],"Ban");

//GeZiManager.PCP.KTT-=1;
this.ST();
this.QK();
MessageCenter.MakeMessage("UIManager","change",1);
}


dA2(){
    MessageCenter.MakeGMessage("NWSU",[GeZiManager.PCP.ZB],NWshopM.NWN[1],GeZiManager.PCP.Pturn,"buy")
   // GeZiManager.PCP.KTT-=1
    this.ST();
    this.QK();
    MessageCenter.MakeMessage("UIManager","change",1);

}
    dA3(){

        MessageCenter.MakeGMessage("NWSU",[GeZiManager.PCP.ZB],NWshopM.NWN[2],GeZiManager.PCP.Pturn,"buy")
      //  GeZiManager.PCP.KTT-=1
        this.ST();
        this.QK();
        MessageCenter.MakeMessage("UIManager","change",1);


    }

    dA4(){

        MessageCenter.MakeGMessage("NWSU",[GeZiManager.PCP.ZB],NWshopM.NWN[3],GeZiManager.PCP.Pturn,"buy")
       // GeZiManager.PCP.KTT-=1
        this.ST();
        this.QK();
        MessageCenter.MakeMessage("UIManager","change",1);
           }
    
       
    
    
    
    
    
        dTS(){
    
            MessageCenter.MakeGMessage("NWSU",[GeZiManager.PCP.ZB],NWshopM.NWN[4],GeZiManager.PCP.Pturn,"buy")
        //    GeZiManager.PCP.KTT-=1
        this.ST();
        this.QK();
        MessageCenter.MakeMessage("UIManager","change",1);
        }





            ST(){
               // this.A1.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.TS[0]+","+NWshopM.nss[0]
               // this.A2.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.TS[1]+","+NWshopM.nss[1];
              //  this.A3.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.TS[2]+","+NWshopM.nss[2];
              //  this.A4.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.TS[3]+","+NWshopM.nss[3];
               // this.A5.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.TS[4]+","+NWshopM.nss[4];
               
               // this.A1.getChildByName("New Sprite").getComponent(cc.Sprite).spriteFrame=this.AM.getComponent(Shops).PM(NWshopM.TS[0]);
               // this.A2.getChildByName("New Sprite").getComponent(cc.Sprite).spriteFrame=this.AM.getComponent(Shops).PM(NWshopM.TS[1]);
               // this.A3.getChildByName("New Sprite").getComponent(cc.Sprite).spriteFrame=this.AM.getComponent(Shops).PM(NWshopM.TS[2]);
              //  this.A4.getChildByName("New Sprite").getComponent(cc.Sprite).spriteFrame=this.AM.getComponent(Shops).PM(NWshopM.TS[3]);
        
              //  this.A5.getChildByName("New Sprite").getComponent(cc.Sprite).spriteFrame=this.AM.getComponent(Shops).PM(NWshopM.TS[4]);
              NWshopM.getBNWN();
              NWshopM.getRNWN();     

            // let o=GeZiManager
              this.NWj.getComponent(cc.Button).interactable=true
              this.XJ.children[0].getComponent(cc.Button).interactable=true
              let j=0
              if(turn.turn%10==0){j=-1}
              let a=NWshopM.BNWN
if (GeZiManager.PCP.color=="Red") {
    a=NWshopM.RNWN
}
                this.NWj.parent.getChildByName("qi").children[0].getComponent(cc.Label).string=a.toString()
              if (GeZiManager.PCP.qi<a+GeZiManager.free+j) {
    this.NWj.getComponent(cc.Button).interactable=false
}

if (GeZiManager.PCP.TN<this.getTNC(5)) {
    this.NWj.getComponent(cc.Button).interactable=false
    this.XJ.children[0].getComponent(cc.Button).interactable=false
}else



this.XJ.getChildByName("Xlabel").getComponent(cc.Label).string=NWshopM.NGS.toString()

   /*            let a
            switch (turn.turn) {
                case 1:a=NWshopM.k1
                    break;
                    case 2:a=NWshopM.k2
                    break;
                    case 3:a=NWshopM.k3
                    break;
                    case 4:a=NWshopM.k4
                    break;
                    case 5:a=NWshopM.k5
                    break;
                    case 6:a=NWshopM.k6
                    break;
                default:a=NWshopM.k6
                    break;
            }   
                this.A1.getChildByName("DeA").getComponent(cc.Label).string=a[0]
                this.A2.getChildByName("DeA").getComponent(cc.Label).string=a[1]
                this.A3.getChildByName("DeA").getComponent(cc.Label).string=a[2]
                this.A4.getChildByName("DeA").getComponent(cc.Label).string=a[3]
                this.A5.getChildByName("DeA").getComponent(cc.Label).string=a[4]


              console.log(NWshopM.k1)
              console.log(NWshopM.k2)
              console.log(NWshopM.k3)
              console.log(NWshopM.k4)
              console.log(NWshopM.k5)
              console.log(NWshopM.k6)



//this.node.getChildByName("New Label").getComponent(cc.Label).string=GeZiManager.PCP.KTT+""
if(GeZiManager.PCP.KTT<=0){for(let a of [this.A1,this.A2,this.A3,this.A4,this.A5]){a.getChildByName("New Button").getComponent(CustomButton).disableButton()}}
    else  {for(let a of [this.A1,this.A2,this.A3,this.A4,this.A5]){a.getChildByName("New Button").getComponent(CustomButton).enableButton()}}   
    
    
    this.show()*/
    for (let index = 1; index <= 6; index++) {
        this.node.children[5].children[index].active=false
         
     }
}

            
show(){
    for (let a of [this.A1,this.A3,this.A2,this.A4,this.A5]) {
        a.getChildByName("who").active=true
       
      }
      

    if (NWshopM.nss[0]%2==1) {
    this.A1.getChildByName("who").getComponent(gif).changeF(0,1);  
}else{ if(NWshopM.nss[0]==0){this.A1.getChildByName("who").active=false}this.A1.getChildByName("who").getComponent(gif).changeF(3,1); }
if (NWshopM.nss[1]%2==1) {
    this.A2.getChildByName("who").getComponent(gif).changeF(0,1);  
}else{ if(NWshopM.nss[1]==0){this.A2.getChildByName("who").active=false}this.A2.getChildByName("who").getComponent(gif).changeF(3,1); }
if (NWshopM.nss[2]%2==1) {
    this.A3.getChildByName("who").getComponent(gif).changeF(0,1);  
}else{if(NWshopM.nss[2]==0){this.A3.getChildByName("who").active=false} this.A3.getChildByName("who").getComponent(gif).changeF(3,1); }
if (NWshopM.nss[3]%2==1) {
    this.A4.getChildByName("who").getComponent(gif).changeF(0,1);  
}else{ if(NWshopM.nss[3]==0){this.A4.getChildByName("who").active=false}this.A4.getChildByName("who").getComponent(gif).changeF(3,1); }
if (NWshopM.nss[4]%2==1) {
    this.A5.getChildByName("who").getComponent(gif).changeF(0,1);  
}else{ if(NWshopM.nss[4]==0){this.A5.getChildByName("who").active=false}this.A5.getChildByName("who").getComponent(gif).changeF(3,1); }

let i=0
for (let a of [this.A1,this.A2,this.A3,this.A4,this.A5]) {
    a.getChildByName("who").children[0].getComponent(gif).changeF(NWshopM.nss[i]-1,1);
    i++;
  }

}

getTNC(n:number){

 let k=  GeZiManager.PCP.TNJJ
    let l =GeZiManager.TNC
   if(k[n]+l[n]>=0){return k[n]+l[n]}else {return 0}
   }







QK(){
    NWshopM.NWN=[]
    this.A1.children[0].getComponent(cc.Sprite).spriteFrame= null
       this.A2.children[0].getComponent(cc.Sprite).spriteFrame= null
       this.A3.children[0].getComponent(cc.Sprite).spriteFrame= null
       this.A4.children[0].getComponent(cc.Sprite).spriteFrame= null
       this.A5.children[0].getComponent(cc.Sprite).spriteFrame= null
       this.A1.getChildByName("Xlabel").getComponent(cc.Label).string=null
       this.A2.getChildByName("Xlabel").getComponent(cc.Label).string=null
       this.A3.getChildByName("Xlabel").getComponent(cc.Label).string=null
       this.A4.getChildByName("Xlabel").getComponent(cc.Label).string=null
       this.A5.getChildByName("Xlabel").getComponent(cc.Label).string=null
    }







}



